import axios from 'axios'
import {
  Message
} from 'element-ui'
import { Encrypt, Decrypt } from '@/common/secret.js'

axios.defaults.headers['token'] = '5018eb9b8b6c4478a3ef2be8a470d478'
const instances = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  // baseURL: 'http://192.168.3.214:2223/dev-api/',
  // baseURL: 'http://192.168.3.121:9201/dev-api/',
  timeout: 13000,
  withCredentials: false
})
// 请求拦截器
instances.interceptors.request.use(request => {
  console.log('post请求', request)
  if (request.method === 'post') {
    const jiami11 = Encrypt(JSON.stringify(request.data))
    console.log('加密后的post', request.data, jiami11)
  }

  return request
}, error => {
  return Promise.reject(error)
})
// 相应拦截器
instances.interceptors.response.use(response => {
  // 解密处理
  if (typeof (response.data) === 'string') {
    const decryptData = Decrypt(response.data)
    // console.log('响应解密', decryptData)
    return Promise.resolve(decryptData)
  } else {
    // 正常接口调用
    if (response.status === 200) {
      if (response.data.ret === 'ERROR') {
        Message.info(response.data.msg)
      }
      return Promise.resolve(response.data)
    } else {
      return Promise.reject(Response)
    }
  }
}, error => {
  Message.warning('请求超时,请稍后再试', error)
  return Promise.reject(error)
})

export default instances
